package com.example.lanouhn.firstapp.activity.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.thread.OneThread;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.squareup.picasso.Picasso;

public class StartActivity extends Activity {

    private ImageView imageView;
    private TextView textView;
    private Handler handler = new Handler(){           //第二步
        public void handleMessage(android.os.Message msg) {

            if (msg.what == 0x101 && (int)msg.obj >=0 ) {

                textView.setText((int)msg.obj + "s");
            } else {
                //跳转主页面
                Intent intent = new Intent();
                intent.setClass(StartActivity.this,MainActivity.class);
                startActivity(intent);
                //直接关闭，实现
                finish();
            }

        };
    };

//http://img2.imgtn.bdimg.com/it/u=3145829044,3570958046&fm=21&gp=0.jpg

    private String image_url = "http://img2.imgtn.bdimg.com/it/u=3145829044,3570958046&fm=21&gp=0.jpg";
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);


        imageView = (ImageView) findViewById(R.id.imageView);
        textView = (TextView) findViewById(R.id.textView);


        OneThread mt = new OneThread(handler);
        mt.start();

        //设置进度条
        dialog = new ProgressDialog(this);
        dialog.setTitle("loding.....");
        dialog.setMessage("loding.....");
        dialog.setCancelable(true);

        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        new MyAsyncTask().execute();

        textView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // 跳转
                Intent intent = new Intent();
                intent.setClass(StartActivity.this,MainActivity.class);
                startActivity(intent);
                //直接关闭
                finish();

            }
        });


    }

    public class MyAsyncTask extends AsyncTask<String,Integer,Bitmap> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected Bitmap doInBackground(String... params) {

//            Bitmap bitmap = null;
//            HttpUtils.getBitMap(image_url);


            return HttpUtils.getBitMap(image_url);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
            dialog.dismiss();

        }
    }

}
